package com.eastcom.providor.dao.impl;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.eastcom.bm.atom.Atom_PPMMonitor;
import com.eastcom.canicula.stope.utils.DateUtil;
import com.eastcom.client.PPMmoniorBean;
import com.eastcom.providor.dao.BaseDao;
import com.eastcom.providor.dao.IPPMMonitorDao;

public class PPMMonitorDao extends BaseDao implements IPPMMonitorDao {

	public List<Atom_PPMMonitor> selectHistoryPPMMonitorByParams(String groupId,Date startDate, Date endDate) throws Exception{
		try{
			StringBuffer hql=new StringBuffer("from Atom_PPMMonitor");
			boolean firstCondition=appeandNotNullParam(hql, "id.groupId", groupId, groupId, "=", true);
			firstCondition=appendNotNullDateAfterCondition(hql, "id.updateTime", startDate, firstCondition);
			appendNotNullDateBeforeCondition(hql, "id.updateTime", endDate, firstCondition);
			return getObjects(hql.toString());
		}
		catch(Exception e){
			throw e;
		}
	}

	public List<Atom_PPMMonitor> selectPPMMonitorByParams(String groupId, String granularity, Date upTime) throws Exception {
		try{
			StringBuffer sql=new StringBuffer("select m.* from QA_PPM_MONITOR m");
			boolean firstCondition=appeandNotNullParam(sql, "m.GROUPID", groupId, groupId, "=", true);
			String timeStamp="3600".equals(granularity)?DateUtil.DateToString("yyyy-MM-dd HH:00:00", upTime):DateUtil.DateToString("yyyy-MM-dd 00:00:00", upTime);
			String column="3600".equals(granularity)?"TRUNC(m.UPTIME,'hh')":"TRUNC(m.UPTIME,'dd')";
			firstCondition=this.appeandExpressWhenValueNotNull(sql, column, timeStamp, "to_date('"+timeStamp+"','yyyy-mm-dd hh24:mi:ss')", "=", firstCondition);
			return getSQLObjects(sql.toString(), Atom_PPMMonitor.class);
		}
		catch(Exception e){
			throw e;
		}
	}

	public List<PPMmoniorBean> selectHistoryPPMMonitorBeanByParams(String groupId,Date startDate, Date endDate) throws Exception{
		try{
			List<PPMmoniorBean> ppmmoniorBeans=new LinkedList<PPMmoniorBean>();
			
			String startTime=startDate==null?null:DateUtil.DateToString(datePattern, startDate);
			String endTime=endDate==null?null:DateUtil.DateToString(datePattern, endDate);
			StringBuffer sql=new StringBuffer("select m.*,d.reportname from qa_ppm_monitor m,qa_ppm_define d where m.groupid=d.groupid(+)");
			appeandNotNullParam(sql, "m.groupid", groupId, groupId, "=", false);
			appendNotNullDateAfterCondition(sql, "m.uptime", startTime, false);
			appendNotNullDateBeforeCondition(sql, "m.uptime", endTime, false);
			List<Map> monitorLst=this.getFieldsBySQL(sql.toString(), new String[]{"groupid","uptime","scannertime","dataTime","filesize","fileName","reportname"});
			if(monitorLst!=null&&!monitorLst.isEmpty()){
				for(Map monitor:monitorLst){
				    PPMmoniorBean ppmmoniorBean=new PPMmoniorBean();
				    ppmmoniorBean.setGroupId((String)monitor.get("groupid"));
				    ppmmoniorBean.setReportName((String)monitor.get("reportname"));
				    ppmmoniorBean.setUpdateTime((Date)monitor.get("uptime"));
				    ppmmoniorBean.setDataTime((Date)monitor.get("dataTime"));
				    ppmmoniorBean.setScannerTime((Date)monitor.get("scannertime"));
				    ppmmoniorBean.setFileName((String)monitor.get("fileName"));
				    ppmmoniorBean.setFilesize(monitor.get("filesize")==null?null:monitor.get("filesize").toString());
				    
				    ppmmoniorBeans.add(ppmmoniorBean);
				}
			}
			
			return ppmmoniorBeans;
		}
		catch(Exception e){
			throw e;
		}
	}
	
}
